package com.kookapi.business.service.impl;

import java.util.List;

import com.kookapi.business.service.IApiDefinitionService;
import com.kookapi.business.vo.TestStepVO;
import com.kookapi.common.utils.DateUtils;
import com.kookapi.common.utils.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.kookapi.business.mapper.TestStepMapper;
import com.kookapi.business.domain.TestStep;
import com.kookapi.business.service.ITestStepService;

/**
 * 测试步骤Service业务层处理
 * 
 * @author cleftgou
 * @date 2025-10-07
 */
@Service
public class TestStepServiceImpl implements ITestStepService 
{
    @Autowired
    private TestStepMapper testStepMapper;

    @Autowired
    private IApiDefinitionService apiDefinitionService;

    /**
     * 查询测试步骤
     * 
     * @param id 测试步骤主键
     * @return 测试步骤
     */
    @Override
    public TestStepVO selectTestStepById(Long id)
    {
        // 创建VO对象
        TestStepVO testStepVO = new TestStepVO();

        // 步骤信息
        TestStep testStep = testStepMapper.selectTestStepById(id);
        BeanUtils.copyProperties(testStep, testStepVO);

        // 需要额外查询一下接口名称信息
        String apiName = apiDefinitionService.selectApiDefinitionById(testStep.getApiId()).getName();
        testStepVO.setApiName(apiName);

        return testStepVO;
    }

    /**
     * 查询测试步骤列表
     * 
     * @param testStep 测试步骤
     * @return 测试步骤
     */
    @Override
    public List<TestStep> selectTestStepList(TestStep testStep)
    {
        return testStepMapper.selectTestStepList(testStep);
    }

    /**
     * 新增测试步骤
     * 
     * @param testStep 测试步骤
     * @return 结果
     */
    @Override
    public int insertTestStep(TestStep testStep)
    {
        testStep.setCreateTime(DateUtils.getNowDate());
        testStep.setCreateBy(SecurityUtils.getUsername());
        return testStepMapper.insertTestStep(testStep);
    }

    /**
     * 修改测试步骤
     * 
     * @param testStep 测试步骤
     * @return 结果
     */
    @Override
    public int updateTestStep(TestStep testStep)
    {
        testStep.setUpdateTime(DateUtils.getNowDate());
        testStep.setUpdateBy(SecurityUtils.getUsername());
        return testStepMapper.updateTestStep(testStep);
    }

    /**
     * 批量删除测试步骤
     * 
     * @param ids 需要删除的测试步骤主键
     * @return 结果
     */
    @Override
    public int deleteTestStepByIds(Long[] ids)
    {
        return testStepMapper.deleteTestStepByIds(ids);
    }

    /**
     * 删除测试步骤信息
     * 
     * @param id 测试步骤主键
     * @return 结果
     */
    @Override
    public int deleteTestStepById(Long id)
    {
        return testStepMapper.deleteTestStepById(id);
    }
}
